package com.mall.product.service.impl;

import com.mall.common.constant.ProductConstant;
import com.mall.product.dao.AttrAttrgroupRelationDao;
import com.mall.product.dao.AttrDao;
import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.entity.AttrEntity;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrService;
import com.mall.product.service.CategoryService;
import com.mall.product.vo.AttrGroupRelationVo;
import com.mall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.AttrGroupDao;
import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long menuId) {
        if (menuId == 0) {
            return this.queryPage(params);
        } else {
            String key = (String) params.get("key");
            QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", menuId);
            if (StringUtils.hasText(key)) {
                queryWrapper.and((obj) -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public AttrGroupEntity getAttrGroupById(Long attrGroupId) {
        AttrGroupEntity AttrGroup = this.getById(attrGroupId);
        ArrayList<Long> pathList = new ArrayList<>();
        List<Long> attrGroupPath = getAttrGroupPath(pathList, AttrGroup.getCatelogId());
        Collections.reverse(attrGroupPath);
        AttrGroup.setCatelogPath(attrGroupPath.toArray(new Long[0]));
        return AttrGroup;
    }

    @Override
    public List<Long> getAttrGroupPath(List<Long> attrGroupPathList, Long catId) {
        if (catId != 0) {
            CategoryEntity category = categoryService.getById(catId);
            attrGroupPathList.add(category.getCatId());
            getAttrGroupPath(attrGroupPathList, category.getParentCid());
        }
        return attrGroupPathList;
    }

    @Override
    public void deleteAttrRelation(AttrGroupRelationVo[] attrGroupRelationVos) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.stream(attrGroupRelationVos).map((item) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrGroupDao.deleteAttrRelation(collect);
    }

    /**
     * 获取属性分组里面还没有关联的本分类里面的其他基本属性，方便添加新的关联
     *
     * @param attrGroupId
     * @param params
     * @return
     */
    @Override
    public PageUtils getNoAttrList(Long attrGroupId, Map<String, Object> params) {
        AttrGroupEntity attrGroupEntity = this.getById(attrGroupId);
        QueryWrapper<AttrEntity> query = new QueryWrapper<AttrEntity>().eq("attr_type", ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        query.eq("catelog_id", attrGroupEntity.getCatelogId());
        List<AttrAttrgroupRelationEntity> attrGroupIdList = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
        List<Long> attrIdList = attrGroupIdList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
        if (!attrIdList.isEmpty()) {
            query.notIn("attr_id", attrIdList);
        }
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            query.eq("attr_id", key).or().like("attr_name", key);
        }
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), query);
        return new PageUtils(page);
    }

    /**
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupVo> getWithattrByCatelogId(Long catelogId) {
        List<AttrGroupEntity> attrGroupList = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupVo> attrGroupVos = attrGroupList.stream().map((item) -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            List<AttrAttrgroupRelationEntity> attrAttrgroupRelationList = attrAttrgroupRelationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", item.getAttrGroupId()));
            List<Long> collectIds = attrAttrgroupRelationList.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(attrAttrgroupRelationList)){
                List<AttrEntity> attrEntities = attrService.listByIds(collectIds);
                attrGroupVo.setAttrs(attrEntities);
            }
            return attrGroupVo;
        }).collect(Collectors.toList());
        return attrGroupVos;
    }

}